package ee.siimoliver.chat.GUI;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import net.miginfocom.swing.MigLayout;
import ee.siimoliver.chat.controllers.BusinessLogicController;
import ee.siimoliver.chat.controllers.ChatSessionController;
import ee.siimoliver.chat.utils.Const;

public class TabbedChatPanel extends JTabbedPane {

	private static final long serialVersionUID = 1L;

	public TabbedChatPanel() {
		super();
	}

	public Component add(String title, Component component,
			BusinessLogicController businessLogicController, ChatSessionController chatSessionController) {
		super.add(title, component);
		this.setTabComponentAt(getTabCount() - 1, new TabComponent(this, title, businessLogicController, chatSessionController));
		return component;
	}

	private class TabComponent extends JPanel {
		private final JTabbedPane parentTabPane;
		private BusinessLogicController businessLogicController;
		private ChatSessionController chatSessionController;

		public TabComponent(final JTabbedPane parentTabPane, String titleText, 
				BusinessLogicController businessLogicController, ChatSessionController chatSessionController) {
			super(new MigLayout("", "[]10[]", ""));
			this.parentTabPane = parentTabPane;
			this.businessLogicController = businessLogicController;
			this.chatSessionController = chatSessionController;
			setOpaque(false);
			JLabel title = new JLabel(titleText);
			JButton btnClose = createCloseButton();
			add(title);
			add(btnClose, "east");
//			addMouseListener(new MouseListener() {
//				@Override
//				public void mouseReleased(MouseEvent e) {}
//				@Override
//				public void mousePressed(MouseEvent e) {}
//				@Override
//				public void mouseExited(MouseEvent e) {}
//				@Override
//				public void mouseEntered(MouseEvent e) {}
//				@Override
//				public void mouseClicked(MouseEvent e) {
//					if (e.getModifiers() == MouseEvent.BUTTON2_MASK) {
//						closeTab();
//					}
//				}
//			});
		}

		private void closeTab() {
			int i = parentTabPane.indexOfTabComponent(TabComponent.this);
			if (i != -1) {
				parentTabPane.remove(i);
				businessLogicController.killChatSession(chatSessionController);
			}
		}

		private JButton createCloseButton() {
			JButton btnClose = new JButton(new ImageIcon(Const.IMAGE_ROOT
					+ "btn_close_normal" + Const.IMAGE_EXT));
			btnClose.setContentAreaFilled(false);
			btnClose.setFocusable(false);
			btnClose.setBorder(BorderFactory.createEtchedBorder());
			btnClose.setBorderPainted(false);
			btnClose.setRolloverEnabled(true);
			btnClose.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					closeTab();
				}
			});
			btnClose.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent e) {
					Component component = e.getComponent();
					if (component instanceof AbstractButton) {
						AbstractButton button = (AbstractButton) component;
						button.setBorderPainted(true);
					}
				}

				public void mouseExited(MouseEvent e) {
					Component component = e.getComponent();
					if (component instanceof AbstractButton) {
						AbstractButton button = (AbstractButton) component;
						button.setBorderPainted(false);
					}
				}
			});
			return btnClose;
		}
	}
}
